package com.tma.serviceclient_a;

import com.tma.demoremoteservice.IStockQuoteService;
import com.tma.demoremoteservice.Person;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    static final String TAG = "MainActivity";
    private boolean mBound = false;
    private IStockQuoteService mService;
    private Button mInvokeButton;

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "Remote service is killed or crashed");
            mService = null;
        }
        
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IStockQuoteService.Stub.asInterface(service);
            Toast.makeText(MainActivity.this, "Bound to service", Toast.LENGTH_SHORT).show();
            mInvokeButton.setEnabled(true);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mInvokeButton = (Button) findViewById(R.id.btn_invoke);
        mInvokeButton.setOnClickListener(this);
        mInvokeButton.setEnabled(false);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mBound) {
            unbindService(mServiceConnection);
        }
        mInvokeButton.setEnabled(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent(IStockQuoteService.class.getName());
        mBound = bindService(intent, mServiceConnection, Service.BIND_AUTO_CREATE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_invoke:
            new GetQuoteTask().execute();
            break;

        default:
            break;
        }
    }

    private class GetQuoteTask extends AsyncTask<Void, Void, String> {
        private ProgressDialog mDialog;
        
        @Override
        protected void onPreExecute() {
            mDialog = ProgressDialog.show(MainActivity.this, "", "Invoking...");
        }

        @Override
        protected String doInBackground(Void... params) {
            Person requester = new Person();
            requester.setName("Tom");
            requester.setAge(24);
            try {
                return mService.getQuote("NOKIA", requester);
            } catch (RemoteException e) {
                Log.e(TAG, e.getMessage(), e);
            }
            return "ERROR INVOKING REMOTE SERVICE";
        }

        @Override
        protected void onPostExecute(String result) {
            mDialog.cancel();
            Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
        }
        
    }
}
